package com.example.xianji.activity;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.example.xianji.widgets.CustomProgressDialog;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Main_ZhanNei_DetailsActivity extends Activity implements OnClickListener {
    private ImageView zhannei_details_back;
    //    private SharedPreferences sp;
//    private Editor ed;
    private TextView zhannei_detail_contant, zhannei_details_time;
    private Button button_last_item, button_next_item;
    private CustomProgressDialog mCustomProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__zhan_nei__details);

        zhannei_details_back = (ImageView) findViewById(R.id.zhannei_details_back);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        zhannei_detail_contant = (TextView) findViewById(R.id.zhannei_detail_contant);
        zhannei_details_time = (TextView) findViewById(R.id.zhannei_details_time);
        button_last_item = (Button) findViewById(R.id.button_last_item);
        button_next_item = (Button) findViewById(R.id.button_next_item);

        zhannei_details(getIntent().getIntExtra("zhannei_id", 0) + "");
        zhannei_details_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.zhannei_details_back) {
            Main_ZhanNei_DetailsActivity.this.finish();
        }
    }

    public void zhannei_details(String id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        map.put("nid", id);
        JSONObject params = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.zhannei_detail,
                params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        JSONArray array = response.getJSONArray("data");
                        JSONObject obj = array.getJSONObject(0);
                        JSONObject obj1 = array.getJSONObject(1);
                        zhannei_detail_contant.setText(obj.getString("details"));
                        zhannei_details_time.setText(obj.getString("created_at").substring(0, 10));
                        if (obj1.getString("next").equals("0") && obj1.getString("prev").equals("0")) {
                            button_next_item.setTextColor(getResources().getColor(R.color.black3F));
                            button_last_item.setTextColor(getResources().getColor(R.color.black3F));
                        } else if (obj1.getString("next").equals("0") && !obj1.getString("prev").equals("0")) {
                            button_next_item.setTextColor(getResources().getColor(R.color.black3F));
                            button_last_item.setTextColor(getResources().getColor(R.color.blue22));
                            final String last_id = obj1.getString("prev");

                            button_last_item.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    if (mCustomProgressDialog == null) {
                                        mCustomProgressDialog = CustomProgressDialog.createDialog(Main_ZhanNei_DetailsActivity.this);
                                        mCustomProgressDialog.setCanceledOnTouchOutside(false);
                                        mCustomProgressDialog.show();
                                    }
                                    zhannei_details(last_id);
                                }
                            });

                        } else if (!obj1.getString("next").equals("0") && !obj1.getString("prev").equals("0")) {
                            button_next_item.setTextColor(getResources().getColor(R.color.blue22));
                            button_last_item.setTextColor(getResources().getColor(R.color.blue22));
                            final String next_id = obj1.getString("next");
                            button_next_item.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    if (mCustomProgressDialog == null) {
                                        mCustomProgressDialog = CustomProgressDialog.createDialog(Main_ZhanNei_DetailsActivity.this);
                                        mCustomProgressDialog.setCanceledOnTouchOutside(false);
                                        mCustomProgressDialog.show();
                                    }
                                    zhannei_details(next_id);
                                }
                            });
                            final String last_id = obj1.getString("prev");
                            button_last_item.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    mCustomProgressDialog = CustomProgressDialog.createDialog(Main_ZhanNei_DetailsActivity.this);
                                    mCustomProgressDialog.setCanceledOnTouchOutside(false);
                                    mCustomProgressDialog.show();
                                    zhannei_details(last_id);
                                }
                            });
                        } else if (!obj1.getString("next").equals("0") && obj1.getString("prev").equals("0")) {
                            button_next_item.setTextColor(getResources().getColor(R.color.blue22));
                            button_last_item.setTextColor(getResources().getColor(R.color.black3F));
                            final String next_id = obj1.getString("next");
                            button_next_item.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    mCustomProgressDialog = CustomProgressDialog.createDialog(Main_ZhanNei_DetailsActivity.this);
                                    mCustomProgressDialog.setCanceledOnTouchOutside(false);
                                    mCustomProgressDialog.show();
                                    zhannei_details(next_id);
                                }
                            });

                            button_last_item.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                }
                            });
                        }
                        if (mCustomProgressDialog != null) {
                            mCustomProgressDialog.dismiss();
                            mCustomProgressDialog = null;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (mCustomProgressDialog != null) {
                    mCustomProgressDialog.dismiss();
                    mCustomProgressDialog = null;
                }
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }
}
